import { Body, Controller, Get, Post, Query } from "@nestjs/common";
import {genRes, ResObjPropNameConst} from 'st-common-req'
import { LoginService } from "./login.service";
import {LoginResultMessage} from './consts/login-result-message'

/**
 * 登录控制器
 */
@Controller("login")
export class LoginController {

  constructor(
    private readonly loginService: LoginService
  ) {
  }

  @Post("pwd")
  async loginByPwd(
    @Body("username") username: string,
    @Body("password") password: string
  ) {
    // 通过密码登录
    const userVo = await this.loginService.loginByPwd(username, password);
    // 登录失败
    if (!userVo) return genRes({
      [ResObjPropNameConst.STATUS]: false,
      [ResObjPropNameConst.MESSAGE]: LoginResultMessage.USERNAME_OR_PASSWORD_ERROR,
    });
    // 登录成功
    return genRes({
      [ResObjPropNameConst.MESSAGE]: LoginResultMessage.LOGIN_SUCCESS,
      [ResObjPropNameConst.DATA]: userVo,
    });
  }

  @Get('effective')
  async getLoginEffective (
    @Query('loginCredential') loginCredential: number
  ) {
    return genRes({
      [ResObjPropNameConst.MESSAGE]: LoginResultMessage.LOGIN_CREDENTIAL_EFFECTIVE,
      [ResObjPropNameConst.DATA]: await this.loginService.judgeLoginEffective(loginCredential)
    })
  }

}
